package store.impl;


import modelsa.Connection;
import store.IStoreProvider;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class StoreProvider implements IStoreProvider {
    @Override
    public String getLoginCode() {
        final File file = new File(StoreConstant.LOGIN_FILE);
        final String value = FileParsUtil.getValue(file, StoreConstant.USERNAME_KEY);
        return value;
    }

    @Override
    public String getPassword() {
        final File file = new File(StoreConstant.LOGIN_FILE);
        final String value = FileParsUtil.getValue(file, StoreConstant.PASSWORD_KEY);
        return value;
    }

    @Override
    public List<Connection> getConnections() {
        final File dir = new File(StoreConstant.DATA_DIR);
        final File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith("c_");  //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        final ArrayList<Connection> connections = new ArrayList<Connection>();
        for (File file : files) {
            Connection c = ConnectionParsUtil.parse(file);
            connections.add(c);
        }

        return connections;
    }
}
